var bird = document.querySelector(".bird"),
	web = document.querySelector(".web"),
	bg = document.querySelector(".bg"),
	mark = document.querySelector(".mark"),
	start = document.querySelector(".start"),
	start_btn = document.querySelector(".start_btn"),
	over = document.querySelector(".over"),
	over_text = document.querySelector(".over_text"),
	over_btn = document.querySelector(".over_btn"),
	bird_speed = 0,
	bird_timer,
	pipe_timer,
	move_timer,
	is_true = false,
	score = 0, // 分数
	is_score = true,
	is_start = false;
/*
 * 点击开始游戏
 */
start_btn.addEventListener("click", function() {
	start.style.display = "none";
	is_start = true;
	if(is_start) {
		bird_speed = 0; // 速度清零
		score = 0; // 分数清零
		mark.innerText = score;
		bird.style.top = "";
		bg.innerHTML = "";
		mark.style.display = "block";
		play();
		is_true = false;
	}
})
/*
 * 结束页面点击重新开始
 */
over_btn.addEventListener("click", function() {
	over.style.display = "none";
	start.style.display = "block";

})
// 点击上升
web.addEventListener("click", function() {
	console.log(bird)
	bird.classList.add("bird_fly");
	if(is_true) return;
	bird_speed -= 15;
	if(bird_speed<-10){
		bird_speed=-10;
	}
})

function play() {
	/*
	 * 元素鸟自由落体
	 */
	clearTimeout(bird_timer);
	bird_timer = setInterval(function() {
		bird_speed ++; // 速度
		var free_fall = bird_speed + bird.offsetTop;

		// 判断是否超过视口范围
		//	if(free_fall < 0) {
		//		is_true = true;
		//		bird_speed = 0;
		//		free_fall = 0;
		//		clearInterval(move_timer);
		//		clearInterval(pipe_timer);
		//		
		//	}
		if(free_fall >= bg.offsetHeight - bird.offsetHeight) {
			is_true = true;
			bird_speed = 0;
			bird.classList.remove("bird_fly");
			clearInterval(move_timer);
			clearInterval(pipe_timer);
			clearInterval(bird_timer);
			mark.style.display = "none";
			over_text.innerText = "score:" + score;
			over.style.display = "block";
		}
		bird.style.top = free_fall + "px"; // 移动

	}, 30);

	/*
	 *  随机数
	 */
	function random(min, max) {
		return Math.floor(Math.random() * (max - min + 1) + min);
	}
	/*
	 * 管子
	 */

	function pipe() {
		var topG = document.createElement("div"),
			bottomG = document.createElement("div");

		topG.className = "top_pipe";
		bottomG.className = "bottom_pipe";
		topG.data_left = 100; // 自定义
		topG.setAttribute("data-num", "true");
		//	bottomG.data_left = 100;
		var h = random(80, 250); // 随机高度
		topG.style.height = h + "px"; // 上面管子的高度
		bottomG.style.height = web.offsetHeight - h - 150 + "px"; // 下面管子的高度
		bottomG.style.top = h + 150 + "px";
		bg.appendChild(topG);
		bg.appendChild(bottomG);
	}
	pipe_timer = setInterval(pipe, 2000); // 隔一段时间生成管子
	move_timer = setInterval(function() {
		var all_topG = document.querySelectorAll(".top_pipe"),
			all_botG = document.querySelectorAll(".bottom_pipe");

		for(var i = 0; i < all_topG.length; i++) {
			// 碰撞检测
			if(setBoom(bird, all_topG[i]) || setBoom(bird, all_botG[i])) {
				is_true = true;
				clearInterval(move_timer);
				//			clearInterval(pipe_timer);

			}
			// 管子超出视口范围就删除
			if(all_topG[i].data_left == "-20") {
				bg.removeChild(all_topG[i]);
				bg.removeChild(all_botG[i]);
				continue;
			}
			// 分数
			var bird_l = bird.offsetLeft,
				topPipe = all_topG[i].offsetLeft + all_topG[i].offsetWidth;
				console.log(bird_l);
			// 判断是否超过管子
			if(bird_l > topPipe) {
				
				if(all_topG[i].getAttribute("data-num")) {
					score++;
					all_topG[i].removeAttribute("data-num");
				}
				mark.innerText = score;
				var a=score % 10;
				if(a>5) {
					bg.style.backgroundImage = "url(img/bg_night.png)";
					bird.style.backgroundImage = "url(img/bird0_3.png)";
				} else {
					bg.style.backgroundImage = "url(img/bg_day.png)";
					bird.style.backgroundImage = "url(img/bird0_4.png)";
				}
			}

			// 管子向左移
			all_botG[i].style.left = all_topG[i].style.left = all_topG[i].data_left - 1 + "%";
			all_topG[i].data_left -= 1;
		}
		//	for(var i = 0; i < all_botG.length; i++) {
		//		all_botG[i].style.left = all_botG[i].data_left - 1 + "%";
		//		all_botG[i].data_left -= 1;
		//	}
	}, 40)
}